//
//  LSZPromotionalGraphicDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/10/27.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZPromotionalGraphicDetailsVC.h"
#import <JavaScriptCore/JavaScriptCore.h>
 
@interface LSZPromotionalGraphicDetailsVC ()

@end

@implementation LSZPromotionalGraphicDetailsVC
-(void)clickButReturn {
    _webView.delegate = nil;
    [self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)loadView
{
    [super loadView];
    if ([_url containsString:@"http://fuyin.app.inping.com/movie/play.php"]) {
    }
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.webView stringByEvaluatingJavaScriptFromString:@"pauseVideo();"];
    if (self.value == 1) {
        [_webView reload];
        
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
        _LCPlayer.comeFrome = 0;
    }
    if (self.PlayAudio == 1) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_LCPlayer play];//继续播放
        });
    }
}

- (void)dealloc {
    NSLog(@"__func__dealloc = %s", __func__);
}
- (void)setupNV {
    UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, NAVIGATION_BAR_HEIGHT)];
    viewTop.backgroundColor = RGBColor(224,106,103);
    viewTop.userInteractionEnabled = YES;
    [self.view addSubview:viewTop];
    
    self.labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(125, NAVIGATION_BAR_HEIGHT-35, ScreenWidth-250, 20)];
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:self.labelTitle];
    self.labelTitle.font = FONT_20;
    
    
    UIButton * but = [[UIButton alloc]init];
    but.frame = CGRectMake(0, 0, 100, NAVIGATION_BAR_HEIGHT);
    [but addTarget:self action:@selector(clickButReturn) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:but];
    
    [self setupRightBtn:viewTop];

    
    UIImageView * imageViewReturn = [[UIImageView alloc]initWithFrame:CGRectMake(20, but.frame.size.height-35, 11, 20)];
    imageViewReturn.image = [UIImage imageNamed:@"home_nav_button_back"];
    [viewTop addSubview:imageViewReturn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.value = 0;
    [self setupNV];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT , ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT)];
    _webView.delegate = self;
//    _url = @"http://fuyin.app.inping.com/holiday/index.php?usercode=qktqu1jemv3litn5xd3m?1111";
     NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_url]];//goURLStrings[index]]];//创建URLse
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        @try {
            [self JavaScriptGetOc];
        } @catch (NSException *exception) { }
    }];
}

///创建右侧分享按钮
- (void)setupRightBtn:(UIView *)viewTop {
    UIButton *rightBut = [[UIButton alloc]init];
    [rightBut setTitle:@"分享" forState:UIControlStateNormal];
    rightBut.bounds = CGRectMake(0, 0, 100, NAVIGATION_BAR_HEIGHT);
    [rightBut addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:rightBut];
    [rightBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(viewTop).offset(-16);
        make.bottom.equalTo(viewTop).offset(-9);
    }];
}
-(void)rightBtnAction{
    @try {
        [self shareShengJingMessageWithImageArray:nil url:self.url title:self.labelTitle.text contentMessage:@"九酷福音" type:0];
    } @catch (NSException *exception) {
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.labelTitle.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if ([self.labelTitle.text isEqualToString:@"福音视频"]){
//        [self startAdViewWithHeightScale:0.15 adUnitTag:@"5840732"];
//        self.baiduMobAdFirst.webView = self.webView;
    }
}
- (void)lcPauseVideo
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        for (NSInteger i = 1; i <= 2; i++) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.webView stringByEvaluatingJavaScriptFromString:@"pauseVideo();"];
            });
        }
    }];
}
- (void)lcPlayVideo
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.webView stringByEvaluatingJavaScriptFromString:@"playVideo();"];
    }];
}

- (void)JavaScriptGetOc{
     __weak typeof(self) weakSelf = self;
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
      //clickVideo
    //点击播放视频
    context[@"clickVideo"] = ^() {
        NSArray *args = [JSContext currentArguments];
        dispatch_async(dispatch_get_main_queue(), ^{
            //        [weakSelf lcPauseVideo];
            //        if (weakSelf.baiduMobAdPrerollNative.seconds <= 0) {
            //            [weakSelf loadPrerollNative];
            //        }

                     //这里记录点击视频的方法
                weakSelf.value = 1;
                if (_LCPlayer.kstatus == LCMusicPlayerStatusPlaying ){
                    [_LCPlayer pause];//暂停播放
                    weakSelf.PlayAudio = 1;
                    _LCPlayer.comeFrome = 1;
                }
            for (JSValue *jsVal in args) {
                NSLog(@"%@", jsVal.toString);
            }
            //添加视频播放历史纪录
            #pragma mark -  添加视频播放历史纪录  -
            if (weakSelf.labelTitle.text) {
                NSDictionary *json = @{@"title":weakSelf.labelTitle.text,
                                       @"link":weakSelf.url
                                       };
                NSLog(@"历史纪录.selectedVideoData = %@<><>%@", _LCPlayer.selectedVideoData,json);
                [_LCPlayer.selectedVideoData addObject:json];
                [[NSUserDefaults standardUserDefaults] setObject:_LCPlayer.selectedVideoData.copy forKey:@"TENVIDEOS"];
                NSLog(@"_LCPlayer.selectedVideoData = %@", _LCPlayer.selectedVideoData);

            }else{
                if (weakSelf.url.length > 0) {
                    NSDictionary *json = @{@"title":@"福音视频",
                                           @"link":weakSelf.url
                                           };
                    NSLog(@"历史纪录_LCPlayer.selectedVideoData = %@<><>%@", _LCPlayer.selectedVideoData,json);
                    [_LCPlayer.selectedVideoData addObject:json];
                    [[NSUserDefaults standardUserDefaults] setObject:_LCPlayer.selectedVideoData.copy forKey:@"TENVIDEOS"];
                    NSLog(@"_LCPlayer.selectedVideoData = %@", _LCPlayer.selectedVideoData);
                }
            }
        });

    };
    //点击分享
    context[@"share"] = ^() {
        @try {
            NSArray *args = [JSContext currentArguments];
            dispatch_async(dispatch_get_main_queue(), ^{

                for (JSValue *jsVal in args) {
                    NSLog(@"%@", jsVal.toString);
                }
                NSString * imageStr = [NSString stringWithFormat:@"%@",args[3]];
                imageStr = [imageStr stringByReplacingOccurrencesOfString:@"./" withString:@"/"];
                imageStr = [NSString stringWithFormat:@"%@%@",@"http://fuyin.app.inping.com/movie/",imageStr];
                NSArray* imageArray = @[imageStr];
                NSString * urlStr = [NSString stringWithFormat:@"%@",args[4]];
                NSString * argsOneStr = [NSString stringWithFormat:@"%@",args[1]];
                NSString *argsTwoStr = [NSString stringWithFormat:@"%@",args[2]];//1061
                if (argsTwoStr.length >= 1000) {
                    argsTwoStr = [argsTwoStr substringToIndex:1000];
                }
//                NSString * argszeroStr = [NSString stringWithFormat:@"%@",args[0]];

                [weakSelf shareShengJingMessageWithImageArray:imageArray url:urlStr title:argsOneStr contentMessage:argsTwoStr type:SSDKContentTypeWebPage];
            });
        } @catch (NSException *exception) {
        }
    };
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [super example1];
}

@end
